/**
 * $Id: RegisterCommandValidator.java 19 2011-06-11 04:06:06Z feigme $
 */
package com.iary.interfaces.web.user;

import org.apache.commons.validator.GenericValidator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

@Component
public class RegisterCommandValidator implements Validator {

	public boolean supports(Class<?> clazz) {
		return RegisterCommand.class.isAssignableFrom(clazz);
	}

	public void validate(Object target, Errors errors) {
		RegisterCommand command = (RegisterCommand) target;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email",
				"error.user.email.null", "");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
				"error.user.name.null", "");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
				"error.user.password.null", "");

		if (GenericValidator.isBlankOrNull(command.getConfirmPassword())) {
			errors.rejectValue("confirmPassword",
					"error.user.confirm.password.null", "");
		} else if (!command.getConfirmPassword().equals(command.getPassword())) {
			errors.rejectValue("password", "error.user.password.not.same", "");
		}
	}

}
